from django.contrib.auth.backends import ModelBackend
from user.models import User

# 重写用户认证：用手机号登录，以及常用方法提供给
class PhoneBackend(ModelBackend):
	def authenticate(self, request, phone=None, password=None, **kwargs):
		phone = kwargs.get('phone')
		if phone is None:
			return None
		else:
			try:
				user = User.objects.get(phone=phone)
			except User.DoesNotExist:
				return None
		if user.check_password(password):
			return user

	def get_user(self, user_id):
		try:
			return User.objects.get(id=user_id)
		except User.DoesNotExist:
			return None
